1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gtk.TreeModelIF; 26 27 private import glib.MemorySlice; 28 private import glib.Str; 29 private import glib.c.functions; 30 private import gobject.ObjectG; 31 private import gobject.Signals; 32 private import gobject.Value; 33 private import gtk.TreeIter; 34 private import gtk.TreeModelIF; 35 private import gtk.TreePath; 36 private import gtk.c.functions; 37 public import gtk.c.types; 38 private import std.algorithm; 39 40 41 /** 42 * The tree interface used by GtkTreeView 43 * 44 * The `GtkTreeModel` interface defines a generic tree interface for 45 * use by the `GtkTreeView` widget. It is an abstract interface, and 46 * is designed to be usable with any appropriate data structure. The 47 * programmer just has to implement this interface on their own data 48 * type for it to be viewable by a `GtkTreeView` widget. 49 * 50 * The model is represented as a hierarchical tree of strongly-typed, 51 * columned data. In other words, the model can be seen as a tree where 52 * every node has different values depending on which column is being 53 * queried. The type of data found in a column is determined by using 54 * the GType system (ie. %G_TYPE_INT, %GTK_TYPE_BUTTON, %G_TYPE_POINTER, 55 * etc). The types are homogeneous per column across all nodes. It is 56 * important to note that this interface only provides a way of examining 57 * a model and observing changes. The implementation of each individual 58 * model decides how and if changes are made. 59 * 60 * In order to make life simpler for programmers who do not need to 61 * write their own specialized model, two generic models are provided 62 * — the `GtkTreeStore` and the `GtkListStore`. To use these, the 63 * developer simply pushes data into these models as necessary. These 64 * models provide the data structure as well as all appropriate tree 65 * interfaces. As a result, implementing drag and drop, sorting, and 66 * storing data is trivial. For the vast majority of trees and lists, 67 * these two models are sufficient. 68 * 69 * Models are accessed on a node/column level of granularity. One can 70 * query for the value of a model at a certain node and a certain 71 * column on that node. There are two structures used to reference a 72 * particular node in a model. They are the [struct@Gtk.TreePath] and 73 * the [struct@Gtk.TreeIter] (“iter” is short for iterator). Most of the 74 * interface consists of operations on a [struct@Gtk.TreeIter]. 75 * 76 * A path is essentially a potential node. It is a location on a model 77 * that may or may not actually correspond to a node on a specific 78 * model. A [struct@Gtk.TreePath] can be converted into either an 79 * array of unsigned integers or a string. The string form is a list 80 * of numbers separated by a colon. Each number refers to the offset 81 * at that level. Thus, the path `0` refers to the root 82 * node and the path `2:4` refers to the fifth child of 83 * the third node. 84 * 85 * By contrast, a [struct@Gtk.TreeIter] is a reference to a specific node on 86 * a specific model. It is a generic struct with an integer and three 87 * generic pointers. These are filled in by the model in a model-specific 88 * way. One can convert a path to an iterator by calling 89 * gtk_tree_model_get_iter(). These iterators are the primary way 90 * of accessing a model and are similar to the iterators used by 91 * `GtkTextBuffer`. They are generally statically allocated on the 92 * stack and only used for a short time. The model interface defines 93 * a set of operations using them for navigating the model. 94 * 95 * It is expected that models fill in the iterator with private data. 96 * For example, the `GtkListStore` model, which is internally a simple 97 * linked list, stores a list node in one of the pointers. The 98 * `GtkTreeModel`Sort stores an array and an offset in two of the 99 * pointers. Additionally, there is an integer field. This field is 100 * generally filled with a unique stamp per model. This stamp is for 101 * catching errors resulting from using invalid iterators with a model. 102 * 103 * The lifecycle of an iterator can be a little confusing at first. 104 * Iterators are expected to always be valid for as long as the model 105 * is unchanged (and doesn’t emit a signal). The model is considered 106 * to own all outstanding iterators and nothing needs to be done to 107 * free them from the user’s point of view. Additionally, some models 108 * guarantee that an iterator is valid for as long as the node it refers 109 * to is valid (most notably the `GtkTreeStore` and `GtkListStore`). 110 * Although generally uninteresting, as one always has to allow for 111 * the case where iterators do not persist beyond a signal, some very 112 * important performance enhancements were made in the sort model. 113 * As a result, the %GTK_TREE_MODEL_ITERS_PERSIST flag was added to 114 * indicate this behavior. 115 * 116 * To help show some common operation of a model, some examples are 117 * provided. The first example shows three ways of getting the iter at 118 * the location `3:2:5`. While the first method shown is 119 * easier, the second is much more common, as you often get paths from 120 * callbacks. 121 * 122 * ## Acquiring a `GtkTreeIter` 123 * 124 * ```c 125 * // Three ways of getting the iter pointing to the location 126 * GtkTreePath *path; 127 * GtkTreeIter iter; 128 * GtkTreeIter parent_iter; 129 * 130 * // get the iterator from a string 131 * gtk_tree_model_get_iter_from_string (model, 132 * &iter, 133 * "3:2:5"); 134 * 135 * // get the iterator from a path 136 * path = gtk_tree_path_new_from_string ("3:2:5"); 137 * gtk_tree_model_get_iter (model, &iter, path); 138 * gtk_tree_path_free (path); 139 * 140 * // walk the tree to find the iterator 141 * gtk_tree_model_iter_nth_child (model, &iter, 142 * NULL, 3); 143 * parent_iter = iter; 144 * gtk_tree_model_iter_nth_child (model, &iter, 145 * &parent_iter, 2); 146 * parent_iter = iter; 147 * gtk_tree_model_iter_nth_child (model, &iter, 148 * &parent_iter, 5); 149 * ``` 150 * 151 * This second example shows a quick way of iterating through a list 152 * and getting a string and an integer from each row. The 153 * populate_model() function used below is not 154 * shown, as it is specific to the `GtkListStore`. For information on 155 * how to write such a function, see the `GtkListStore` documentation. 156 * 157 * ## Reading data from a `GtkTreeModel` 158 * 159 * ```c 160 * enum 161 * { 162 * STRING_COLUMN, 163 * INT_COLUMN, 164 * N_COLUMNS 165 * }; 166 * 167 * ... 168 * 169 * GtkTreeModel *list_store; 170 * GtkTreeIter iter; 171 * gboolean valid; 172 * int row_count = 0; 173 * 174 * // make a new list_store 175 * list_store = gtk_list_store_new (N_COLUMNS, 176 * G_TYPE_STRING, 177 * G_TYPE_INT); 178 * 179 * // Fill the list store with data 180 * populate_model (list_store); 181 * 182 * // Get the first iter in the list, check it is valid and walk 183 * // through the list, reading each row. 184 * 185 * valid = gtk_tree_model_get_iter_first (list_store, 186 * &iter); 187 * while (valid) 188 * { 189 * char *str_data; 190 * int int_data; 191 * 192 * // Make sure you terminate calls to gtk_tree_model_get() with a “-1” value 193 * gtk_tree_model_get (list_store, &iter, 194 * STRING_COLUMN, &str_data, 195 * INT_COLUMN, &int_data, 196 * -1); 197 * 198 * // Do something with the data 199 * g_print ("Row %d: (%s,%d)\n", 200 * row_count, str_data, int_data); 201 * g_free (str_data); 202 * 203 * valid = gtk_tree_model_iter_next (list_store, 204 * &iter); 205 * row_count++; 206 * } 207 * ``` 208 * 209 * The `GtkTreeModel` interface contains two methods for reference 210 * counting: gtk_tree_model_ref_node() and gtk_tree_model_unref_node(). 211 * These two methods are optional to implement. The reference counting 212 * is meant as a way for views to let models know when nodes are being 213 * displayed. `GtkTreeView` will take a reference on a node when it is 214 * visible, which means the node is either in the toplevel or expanded. 215 * Being displayed does not mean that the node is currently directly 216 * visible to the user in the viewport. Based on this reference counting 217 * scheme a caching model, for example, can decide whether or not to cache 218 * a node based on the reference count. A file-system based model would 219 * not want to keep the entire file hierarchy in memory, but just the 220 * folders that are currently expanded in every current view. 221 * 222 * When working with reference counting, the following rules must be taken 223 * into account: 224 * 225 * - Never take a reference on a node without owning a reference on its parent. 226 * This means that all parent nodes of a referenced node must be referenced 227 * as well. 228 * 229 * - Outstanding references on a deleted node are not released. This is not 230 * possible because the node has already been deleted by the time the 231 * row-deleted signal is received. 232 * 233 * - Models are not obligated to emit a signal on rows of which none of its 234 * siblings are referenced. To phrase this differently, signals are only 235 * required for levels in which nodes are referenced. For the root level 236 * however, signals must be emitted at all times (however the root level 237 * is always referenced when any view is attached). 238 */ 239 public interface TreeModelIF{ 240 /** Get the main Gtk struct */ 241 public GtkTreeModel* getTreeModelStruct(bool transferOwnership = false); 242 243 /** the main Gtk struct as a void* */ 244 protected void* getStruct(); 245 246 247 /** */ 248 public static GType getType() 249 { 250 return gtk_tree_model_get_type(); 251 } 252 253 /** 254 * Creates a new `GtkTreeModel`, with @child_model as the child_model 255 * and @root as the virtual root. 256 * 257 * Params: 258 * root = A `GtkTreePath` 259 * 260 * Returns: A new `GtkTreeModel`. 261 */ 262 public TreeModelIF filterNew(TreePath root); 263 264 alias foreac = foreach_; 265 /** 266 * Calls @func on each node in model in a depth-first fashion. 267 * 268 * If @func returns %TRUE, then the tree ceases to be walked, 269 * and gtk_tree_model_foreach() returns. 270 * 271 * Params: 272 * func = a function to be called on each row 273 * userData = user data to passed to @func 274 */ 275 public void foreach_(GtkTreeModelForeachFunc func, void* userData); 276 277 /** 278 * Returns the type of the column. 279 * 280 * Params: 281 * index = the column index 282 * 283 * Returns: the type of the column 284 */ 285 public GType getColumnType(int index); 286 287 /** 288 * Returns a set of flags supported by this interface. 289 * 290 * The flags are a bitwise combination of `GtkTreeModel`Flags. 291 * The flags supported should not change during the lifetime 292 * of the @tree_model. 293 * 294 * Returns: the flags supported by this interface 295 */ 296 public GtkTreeModelFlags getFlags(); 297 298 /** 299 * Sets @iter to a valid iterator pointing to @path. 300 * 301 * If @path does not exist, @iter is set to an invalid 302 * iterator and %FALSE is returned. 303 * 304 * Params: 305 * iter = the uninitialized `GtkTreeIter` 306 * path = the `GtkTreePath` 307 * 308 * Returns: %TRUE, if @iter was set 309 */ 310 public bool getIter(out TreeIter iter, TreePath path); 311 312 /** 313 * Initializes @iter with the first iterator in the tree 314 * (the one at the path "0"). 315 * 316 * Returns %FALSE if the tree is empty, %TRUE otherwise. 317 * 318 * Params: 319 * iter = the uninitialized `GtkTreeIter` 320 * 321 * Returns: %TRUE, if @iter was set 322 */ 323 public bool getIterFirst(out TreeIter iter); 324 325 /** 326 * Sets @iter to a valid iterator pointing to @path_string, if it 327 * exists. 328 * 329 * Otherwise, @iter is left invalid and %FALSE is returned. 330 * 331 * Params: 332 * iter = an uninitialized `GtkTreeIter` 333 * pathString = a string representation of a `GtkTreePath` 334 * 335 * Returns: %TRUE, if @iter was set 336 */ 337 public bool getIterFromString(out TreeIter iter, string pathString); 338 339 /** 340 * Returns the number of columns supported by @tree_model. 341 * 342 * Returns: the number of columns 343 */ 344 public int getNColumns(); 345 346 /** 347 * Returns a newly-created `GtkTreePath` referenced by @iter. 348 * 349 * This path should be freed with gtk_tree_path_free(). 350 * 351 * Params: 352 * iter = the `GtkTreeIter` 353 * 354 * Returns: a newly-created `GtkTreePath` 355 */ 356 public TreePath getPath(TreeIter iter); 357 358 /** 359 * Generates a string representation of the iter. 360 * 361 * This string is a “:” separated list of numbers. 362 * For example, “4:10:0:3” would be an acceptable 363 * return value for this string. 364 * 365 * Params: 366 * iter = a `GtkTreeIter` 367 * 368 * Returns: a newly-allocated string 369 */ 370 public string getStringFromIter(TreeIter iter); 371 372 /** 373 * Gets the value of one or more cells in the row referenced by @iter. 374 * 375 * See [method@Gtk.TreeModel.get], this version takes a va_list 376 * for language bindings to use. 377 * 378 * Params: 379 * iter = a row in @tree_model 380 * varArgs = va_list of column/return location pairs 381 */ 382 public void getValist(TreeIter iter, void* varArgs); 383 384 /** 385 * Initializes and sets @value to that at @column. 386 * 387 * When done with @value, g_value_unset() needs to be called 388 * to free any allocated memory. 389 * 390 * Params: 391 * iter = the `GtkTreeIter` 392 * column = the column to lookup the value at 393 * value = an empty `GValue` to set 394 */ 395 public void getValue(TreeIter iter, int column, out Value value); 396 397 /** 398 * Sets @iter to point to the first child of @parent. 399 * 400 * If @parent has no children, %FALSE is returned and @iter is 401 * set to be invalid. @parent will remain a valid node after this 402 * function has been called. 403 * 404 * If @parent is %NULL returns the first node, equivalent to 405 * `gtk_tree_model_get_iter_first (tree_model, iter);` 406 * 407 * Params: 408 * iter = the new `GtkTreeIter` to be set to the child 409 * parent = the `GtkTreeIter` 410 * 411 * Returns: %TRUE, if @iter has been set to the first child 412 */ 413 public bool iterChildren(out TreeIter iter, TreeIter parent); 414 415 /** 416 * Returns %TRUE if @iter has children, %FALSE otherwise. 417 * 418 * Params: 419 * iter = the `GtkTreeIter` to test for children 420 * 421 * Returns: %TRUE if @iter has children 422 */ 423 public bool iterHasChild(TreeIter iter); 424 425 /** 426 * Returns the number of children that @iter has. 427 * 428 * As a special case, if @iter is %NULL, then the number 429 * of toplevel nodes is returned. 430 * 431 * Params: 432 * iter = the `GtkTreeIter` 433 * 434 * Returns: the number of children of @iter 435 */ 436 public int iterNChildren(TreeIter iter); 437 438 /** 439 * Sets @iter to point to the node following it at the current level. 440 * 441 * If there is no next @iter, %FALSE is returned and @iter is set 442 * to be invalid. 443 * 444 * Params: 445 * iter = the `GtkTreeIter` 446 * 447 * Returns: %TRUE if @iter has been changed to the next node 448 */ 449 public bool iterNext(TreeIter iter); 450 451 /** 452 * Sets @iter to be the child of @parent, using the given index. 453 * 454 * The first index is 0. If @n is too big, or @parent has no children, 455 * @iter is set to an invalid iterator and %FALSE is returned. @parent 456 * will remain a valid node after this function has been called. As a 457 * special case, if @parent is %NULL, then the @n-th root node 458 * is set. 459 * 460 * Params: 461 * iter = the `GtkTreeIter` to set to the nth child 462 * parent = the `GtkTreeIter` to get the child from 463 * n = the index of the desired child 464 * 465 * Returns: %TRUE, if @parent has an @n-th child 466 */ 467 public bool iterNthChild(out TreeIter iter, TreeIter parent, int n); 468 469 /** 470 * Sets @iter to be the parent of @child. 471 * 472 * If @child is at the toplevel, and doesn’t have a parent, then 473 * @iter is set to an invalid iterator and %FALSE is returned. 474 * @child will remain a valid node after this function has been 475 * called. 476 * 477 * @iter will be initialized before the lookup is performed, so @child 478 * and @iter cannot point to the same memory location. 479 * 480 * Params: 481 * iter = the new `GtkTreeIter` to set to the parent 482 * child = the `GtkTreeIter` 483 * 484 * Returns: %TRUE, if @iter is set to the parent of @child 485 */ 486 public bool iterParent(out TreeIter iter, TreeIter child); 487 488 /** 489 * Sets @iter to point to the previous node at the current level. 490 * 491 * If there is no previous @iter, %FALSE is returned and @iter is 492 * set to be invalid. 493 * 494 * Params: 495 * iter = the `GtkTreeIter` 496 * 497 * Returns: %TRUE if @iter has been changed to the previous node 498 */ 499 public bool iterPrevious(TreeIter iter); 500 501 /** 502 * Lets the tree ref the node. 503 * 504 * This is an optional method for models to implement. 505 * To be more specific, models may ignore this call as it exists 506 * primarily for performance reasons. 507 * 508 * This function is primarily meant as a way for views to let 509 * caching models know when nodes are being displayed (and hence, 510 * whether or not to cache that node). Being displayed means a node 511 * is in an expanded branch, regardless of whether the node is currently 512 * visible in the viewport. For example, a file-system based model 513 * would not want to keep the entire file-hierarchy in memory, 514 * just the sections that are currently being displayed by 515 * every current view. 516 * 517 * A model should be expected to be able to get an iter independent 518 * of its reffed state. 519 * 520 * Params: 521 * iter = the `GtkTreeIter` 522 */ 523 public void refNode(TreeIter iter); 524 525 /** 526 * Emits the ::row-changed signal on @tree_model. 527 * 528 * See [signal@Gtk.TreeModel::row-changed]. 529 * 530 * Params: 531 * path = a `GtkTreePath` pointing to the changed row 532 * iter = a valid `GtkTreeIter` pointing to the changed row 533 */ 534 public void rowChanged(TreePath path, TreeIter iter); 535 536 /** 537 * Emits the ::row-deleted signal on @tree_model. 538 * 539 * See [signal@Gtk.TreeModel::row-deleted]. 540 * 541 * This should be called by models after a row has been removed. 542 * The location pointed to by @path should be the location that 543 * the row previously was at. It may not be a valid location anymore. 544 * 545 * Nodes that are deleted are not unreffed, this means that any 546 * outstanding references on the deleted node should not be released. 547 * 548 * Params: 549 * path = a `GtkTreePath` pointing to the previous location of 550 * the deleted row 551 */ 552 public void rowDeleted(TreePath path); 553 554 /** 555 * Emits the ::row-has-child-toggled signal on @tree_model. 556 * 557 * See [signal@Gtk.TreeModel::row-has-child-toggled]. 558 * 559 * This should be called by models after the child 560 * state of a node changes. 561 * 562 * Params: 563 * path = a `GtkTreePath` pointing to the changed row 564 * iter = a valid `GtkTreeIter` pointing to the changed row 565 */ 566 public void rowHasChildToggled(TreePath path, TreeIter iter); 567 568 /** 569 * Emits the ::row-inserted signal on @tree_model. 570 * 571 * See [signal@Gtk.TreeModel::row-inserted]. 572 * 573 * Params: 574 * path = a `GtkTreePath` pointing to the inserted row 575 * iter = a valid `GtkTreeIter` pointing to the inserted row 576 */ 577 public void rowInserted(TreePath path, TreeIter iter); 578 579 /** 580 * Emits the ::rows-reordered signal on @tree_model. 581 * 582 * See [signal@Gtk.TreeModel::rows-reordered]. 583 * 584 * This should be called by models when their rows have been 585 * reordered. 586 * 587 * Params: 588 * path = a `GtkTreePath` pointing to the tree node whose children 589 * have been reordered 590 * iter = a valid `GtkTreeIter` pointing to the node whose children 591 * have been reordered, or %NULL if the depth of @path is 0 592 * newOrder = an array of integers mapping the current position of 593 * each child to its old position before the re-ordering, 594 * i.e. @new_order`[newpos] = oldpos` 595 */ 596 public void rowsReordered(TreePath path, TreeIter iter, int* newOrder); 597 598 /** 599 * Emits the ::rows-reordered signal on @tree_model. 600 * 601 * See [signal@Gtk.TreeModel::rows-reordered]. 602 * 603 * This should be called by models when their rows have been 604 * reordered. 605 * 606 * Params: 607 * path = a `GtkTreePath` pointing to the tree node whose children 608 * have been reordered 609 * iter = a valid `GtkTreeIter` pointing to the node 610 * whose children have been reordered, or %NULL if the depth 611 * of @path is 0 612 * newOrder = an array of integers 613 * mapping the current position of each child to its old 614 * position before the re-ordering, 615 * i.e. @new_order`[newpos] = oldpos` 616 */ 617 public void rowsReorderedWithLength(TreePath path, TreeIter iter, int[] newOrder); 618 619 /** 620 * Lets the tree unref the node. 621 * 622 * This is an optional method for models to implement. 623 * To be more specific, models may ignore this call as it exists 624 * primarily for performance reasons. For more information on what 625 * this means, see gtk_tree_model_ref_node(). 626 * 627 * Please note that nodes that are deleted are not unreffed. 628 * 629 * Params: 630 * iter = the `GtkTreeIter` 631 */ 632 public void unrefNode(TreeIter iter); 633 634 /** 635 * This signal is emitted when a row in the model has changed. 636 * 637 * Params: 638 * path = a `GtkTreePath` identifying the changed row 639 * iter = a valid `GtkTreeIter` pointing to the changed row 640 */ 641 gulong addOnRowChanged(void delegate(TreePath, TreeIter, TreeModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0); 642 643 /** 644 * This signal is emitted when a row has been deleted. 645 * 646 * Note that no iterator is passed to the signal handler, 647 * since the row is already deleted. 648 * 649 * This should be called by models after a row has been removed. 650 * The location pointed to by @path should be the location that 651 * the row previously was at. It may not be a valid location anymore. 652 * 653 * Params: 654 * path = a `GtkTreePath` identifying the row 655 */ 656 gulong addOnRowDeleted(void delegate(TreePath, TreeModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0); 657 658 /** 659 * This signal is emitted when a row has gotten the first child 660 * row or lost its last child row. 661 * 662 * Params: 663 * path = a `GtkTreePath` identifying the row 664 * iter = a valid `GtkTreeIter` pointing to the row 665 */ 666 gulong addOnRowHasChildToggled(void delegate(TreePath, TreeIter, TreeModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0); 667 668 /** 669 * This signal is emitted when a new row has been inserted in 670 * the model. 671 * 672 * Note that the row may still be empty at this point, since 673 * it is a common pattern to first insert an empty row, and 674 * then fill it with the desired values. 675 * 676 * Params: 677 * path = a `GtkTreePath` identifying the new row 678 * iter = a valid `GtkTreeIter` pointing to the new row 679 */ 680 gulong addOnRowInserted(void delegate(TreePath, TreeIter, TreeModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0); 681 682 /** 683 * This signal is emitted when the children of a node in the 684 * `GtkTreeModel` have been reordered. 685 * 686 * Note that this signal is not emitted 687 * when rows are reordered by DND, since this is implemented 688 * by removing and then reinserting the row. 689 * 690 * Params: 691 * path = a `GtkTreePath` identifying the tree node whose children 692 * have been reordered 693 * iter = a valid `GtkTreeIter` pointing to the node whose children 694 * have been reordered, or %NULL if the depth of @path is 0 695 * newOrder = an array of integers mapping the current position 696 * of each child to its old position before the re-ordering, 697 * i.e. @new_order`[newpos] = oldpos` 698 */ 699 gulong addOnRowsReordered(void delegate(TreePath, TreeIter, void*, TreeModelIF) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0); 700 }